package com.czh.gzskzn.shenke3.Activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.czh.gzskzn.shenke3.Activity.UtilServer.WanZhi;
import com.czh.gzskzn.shenke3.Activity.UtilT.Ting;
import com.czh.gzskzn.shenke3.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by gzsk on 2017/4/25.
 * name  陈忠红
 * email czhhx15599109256@163.com
 */

public class GonGaoActivity extends AppCompatActivity {

    private   ListView listView;
    private LinearLayout Bake;

    ArrayList<Ting> list = new ArrayList<>();
    private int i;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gon_activity);
        inn();

        dianJi();

    }

    private void dianJi() {

        Bake.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void inn() {
        listView = (ListView) findViewById(R.id.lst_tinshui);
        Bake = (LinearLayout) findViewById(R.id.ll_tonzhi_baike);

        MyBaseAdater adapter = new MyBaseAdater();
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(GonGaoActivity.this, GongaoYe.class);
//                intent.putExtra("id",list.get(position).getId());
                startActivity(intent);
            }
        });
    }

    class MyBaseAdater extends BaseAdapter {
        @Override
        public int getCount() {
            return 3;
//            return 1;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final Zhidai zhidai ;



            if (convertView == null) {
                zhidai = new Zhidai();
                convertView = View.inflate(GonGaoActivity.this, R.layout.listitme, null);
                zhidai.title = (TextView) convertView.findViewById(R.id.tv_ts_biaoti);
                zhidai.source = (TextView) convertView.findViewById(R.id.tv_ts_shijian);


                OkGo.get(WanZhi.TONZHIDZHI)
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                list = (ArrayList<Ting>) getListPersonByGson(s);
                                for (i = 0; i < list.size(); i++) {
                                    Log.e("ceshi", list.get(i).getDate());
                                    String title1 = list.get(i).getTitle();
                                    String date = list.get(i).getDate();
                                    zhidai.title.setText(title1);
                                    zhidai.source.setText(date);

                                }

                                Log.e("ceshi", list.get(1).getContent());

                            }

                        });
                convertView.setTag(zhidai);
            } else {
                zhidai = (Zhidai) convertView.getTag();
            }
            return convertView;
        }

        class Zhidai {
            TextView title;
            TextView source;
            ImageView firstImg;
        }
    }

    //通过Gson解析
    public List<Ting> getListPersonByGson(String jsony) {
        list = new ArrayList<Ting>();
        Gson gson = new Gson();
        list = gson.fromJson(jsony, new TypeToken<List<Ting>>() {
        }.getType());
        return list;
    }

}

